import VueRouter from "vue-router";
import Staff from "../pages/staff"
import CatcoffeeProduct from "../pages/product"
import User from "../pages/user"
import CatcoffeeOrder from "../pages/order"
import Login from "../pages/login"
import Index from "../pages/index"
import CatcoffeeClassification from "../pages/classification"
import Visualization from "../pages/visualization"
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
const router = new VueRouter({
    mode: "history",
    redirect: 'index',
    routes: [
        {
            path:'/',
            redirect:'product'//默认路由
        },
        {
            path: "/login",
            component: Login
        },
        {
            path: "/index",
            component: Index,
            children: [{
                path: "/staff",
                component: Staff
            },
            {
                path: "/product",
                component: CatcoffeeProduct
            },
            {
                path: "/classification",
                component: CatcoffeeClassification
            },
            {
                path: "/user",
                component: User
            },
            {
                path: "/order",
                component: CatcoffeeOrder
            },
            {
                path: "/visualization",
                component: Visualization
            },]
        },
    ]
});
router.beforeEach((to, from, back) => {
    if (to.path == '/login') {
        back();
    } else {
        if (window.localStorage.getItem("staff") == null) {
            window.alert("请登录");
            router.push('/login');
            return false;
        } else {
            back();
        }
    }
})
export default router;